package com.liutaw.nanjingair.views.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.liutaw.nanjingair.R;
import com.liutaw.nanjingair.models.WeatherProduct;
import com.liutaw.nanjingair.views.activities.WindActivity;
import com.liutaw.nanjingair.views.activities.products.ImportantWeatherPDFActivity;
import com.liutaw.nanjingair.views.activities.products.RandarActivity;
import com.liutaw.nanjingair.views.activities.products.SatelliteActivity;
import com.liutaw.nanjingair.views.activities.products.TrendForcastActivity;
import com.liutaw.nanjingair.views.activities.products.WeatherForcastActiviy;
import com.liutaw.nanjingair.views.activities.products.WeatherWaringActivity;

import java.util.List;

/**
 * Created by liutao on 16/2/19.
 */
public class WeatherProductAdapter extends BaseAdapter {
    public static final String WIND_PATH = "台风路径";
    public static final String TRENDING_PREDICTION = "趋势预测";
    public static final String RADAR_PIC = "雷达图像";
    public static final String SATELLITE_PIC = "卫星云图";
    public static final String WEATHER_FORCAST = "天气预报";
    public static final String IMPONENT_WEATHER = "重要天气";


    private Context context;
    private List<WeatherProduct> weatherProductList;

    public WeatherProductAdapter(Context context, List<WeatherProduct> weatherProductList) {
        this.context = context;
        this.weatherProductList = weatherProductList;
//        weatherProductList = new ArrayList<>();
//        initData();
    }

//    private void initData() {
//        weatherProductList.add(WeatherProduct.getInstance("天气预报"));
//        weatherProductList.add(WeatherProduct.getInstance("雷达图像"));
//        weatherProductList.add(WeatherProduct.getInstance("卫星云图"));
//        weatherProductList.add(WeatherProduct.getInstance("台风路径"));
//        weatherProductList.add(WeatherProduct.getInstance("趋势预测"));
//        weatherProductList.add(WeatherProduct.getInstance("实况信息"));
//        weatherProductList.add(WeatherProduct.getInstance("统计信息"));
//        weatherProductList.add(WeatherProduct.getInstance("重要天气"));
//        weatherProductList.add(WeatherProduct.getInstance("气候评价"));
//        weatherProductList.add(WeatherProduct.getInstance("气象预警"));
//    }

    @Override
    public int getCount() {
        return weatherProductList.size();
    }

    @Override
    public Object getItem(int position) {
        return weatherProductList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final WeatherProduct content = weatherProductList.get(position);
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_product, null);
            viewHolder = new ViewHolder();
            viewHolder.imgProduct = (ImageView) convertView.findViewById(R.id.img_product);
            viewHolder.textProductName = (TextView) convertView.findViewById(R.id.text_product_name);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.textProductName.setText(content.getName());
        viewHolder.imgProduct.setImageResource(content.getIconRes());

        switch (content.getName()) {
            case WeatherProductAdapter.WIND_PATH:
                convertView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        context.startActivity(new Intent(context, WindActivity.class));
                    }
                });
                break;
            case WeatherProductAdapter.TRENDING_PREDICTION:
                convertView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        context.startActivity(TrendForcastActivity.getCallingIntent(context));
                    }
                });
                break;
            case WeatherProductAdapter.RADAR_PIC:
                convertView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        context.startActivity(RandarActivity.getCallingIntent(context));
                    }
                });
                break;
            case WeatherProductAdapter.WEATHER_FORCAST:
                convertView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        context.startActivity(WeatherForcastActiviy.getCallingIntent(context));
                    }
                });
                break;
            case WeatherProductAdapter.SATELLITE_PIC:
                convertView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        context.startActivity(SatelliteActivity.getCallingIntent(context));
                    }
                });
                break;
            case WeatherProductAdapter.IMPONENT_WEATHER:
                convertView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        context.startActivity(ImportantWeatherPDFActivity.getCallingIntent(context));
                    }
                });
                break;

            case "气象预警":
                convertView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        context.startActivity(WeatherWaringActivity.getCallingIntent(context));
                    }
                });
                break;

            default:
                convertView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(context, content.getName(), Toast.LENGTH_SHORT).show();
                    }
                });
                break;
        }
        return convertView;
    }

    public static final class ViewHolder {
        //        @Bind(R.id.img_product)
        ImageView imgProduct;
        //        @Bind(R.id.text_product_name)
        TextView textProductName;

        public ViewHolder() {

        }
//        ViewHolder(View view) {
//            ButterKnife.bind(this, view);
//        }
    }
}
